import React, { memo, useRef, useEffect } from 'react';
import * as echarts from 'echarts/core';
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent,
} from 'echarts/components';
import { PieChart } from 'echarts/charts';
import { LabelLayout } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';

import './style.less';
echarts.use([
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  PieChart,
  CanvasRenderer,
  LabelLayout,
]);

export default memo(function MyAddressEchart(props) {
  const chartRef = useRef();
  useEffect(() => {
    const myChart = echarts.init(chartRef.current);
    const dataList = [];
    let i = 0;
    if (props.dataList[0]) {
      for (let item of props.dataList) {
        dataList[i] = { name: item.title, value: item.number1 };
        i++;
      }
    }
    const option = {
      title: {
        text: 'Referer of a Website',
        subtext: 'Fake Data',
        left: 'center',
      },
      legend: {
        orient: 'vertical',
        left: 'left',
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: '50%',
          data: dataList,
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)',
            },
          },
        },
      ],
    };

    myChart.setOption(option);
  }, [props]);

  return <div className="my-address-echart" ref={chartRef}></div>;
});
